﻿// ===============================================================================
// DotNet.Platform 开发框架 2016 版权所有
// ===============================================================================

using System;
using System.Collections.Generic;
using System.Linq;
using DotNet.Collections;
using DotNet.Data;
using DotNet.Extensions;
using DotNet.Utility;
using XNWebModel;

namespace XNWebService
{
    /// <summary>
    /// 新闻资讯服务
    /// </summary>
    public class NewsService
    {
        private static readonly NewsService instance = new NewsService();
        private readonly Repository<News> repos = new Repository<News>(new SiteDatabase());


        public static NewsService Instance { get { return instance; } }
        

        /// <summary>
        /// 获取新闻资讯对象
        /// </summary>
        /// <param name="id">新闻资讯主键</param>
        /// <returns>返回新闻资讯对象</returns>
        public News Get(int id)
        {
            return repos.Get(id);
        }
        

        public PageList<News> GetAppPageList(int pageIndex, int pageSize, string categoryId)
        {
            var _categoryId = categoryId.Trim().ToInt();
            var orderName = nameof(News.CreateDateTime);
            var orderDir = nameof(OrderDir.Desc);
            var query = repos.SQL
                .Where(p => p.IsAudit == true)
                .Where(p => p.IsApp == true)
                .Where(p => p.CategoryId == _categoryId)
                .Take(pageSize)
                .Page(pageIndex).OrderBy(orderName, orderDir.IsAsc());

            return repos.Page(query);
        }
    }
}